#!/usr/bin/python3
# -*- coding: UTF-8 -*-

from app import App
from utils.tools import Tools
from utils.element import Element

import gol
gol._init()
from os import popen as osPopen, path as osPath  # 管道处理


class Application(App):
    # 初始配置
    def __init__(self):
        # python2 不支持 super().__init__()
        App().__init__()

        self._d = App()._d
        self.tool = Tools()

    # 左边三线
    def homeLeftBtn(self):
        sTxt = "<<-->>: 金币按钮"
        try:
            self._d(description="火苗管理，按钮").click()
            print("%s - 成功" %(sTxt))
            self.tool.setSleep(3)
            return True
        except:
            print("%s - 失败" %(sTxt))
            return False

    # 火苗管理
    def fireBtn(self):
        sTxt = '<<-->>: 火苗管理'
        try:
            self._d(resourceId="com.ss.android.ugc.live:id/title", text="火苗管理").click()
            print("%s - 成功" %(sTxt))
            self.tool.setSleep(3)
            return True
        except:
            print("%s - 失败" %(sTxt))
            return False
    
    # 限时任务
    def TimeLimitedTask(self):
        sTxt = '<<-->>: 限时任务'
        try:
            self._d(text="每20分钟看1次视频，最高赚1万火苗").right(text="去领取").click()
            print("%s - 成功" %(sTxt))
            self.tool.setSleep(30)
            self._d.press("back")
            print("%s - 完成" %(sTxt))
            return True
        except:
            print("%s - 失败" %(sTxt))
            return False

    # 抽奖
    def Lottery(self):
        sTxt = '<<-->>: 抽奖'
        try:
            self._d(text="火苗天天领，碎片换手机").right(text="去抽奖").click()
            print("%s - 成功" %(sTxt))
            self.tool.setSleep(3)
            return True
        except:
            print("%s - 失败" %(sTxt))
            return False

    # 抽奖 - 转盘
    def Turntable(self):
        xy = self._d.window_size()
        center = xy[0] // 2
        bottom = xy[1] - 150
        tupMove = (center, bottom, center, bottom - 1000)
        self.tool.move(tupMove)


    # 循环视频
    def whileMove(self):
        iCount = 10
        while iCount > 1:
            sVal = self.addCount()
            print('sVal', sVal)
            if sVal == 'done':
                break
            iCount =- 1

    # 点击视频加次数
    def addCount(self):
        sTxt = '<<-->>: 视频+1'
        try:
            self._d(text="观看视频，抽奖次数+1").right(text="去完成").click()
            self.tool.setSleep(30)
            self._d.press("back")
            print("%s - 成功" %(sTxt))
            return 'next'
        except:
            node = self._d(text="观看视频，抽奖次数+1").right(text="已完成").info
            print(node)
            if not node :
                print("%s - 失败" %(sTxt))
                return 'next'
            else:
                print("%s - 完成" %(sTxt))
                return 'done'
            

class Action(App):
    # 初始配置
    def __init__(self):
        # python2 不支持 super().__init__()
        App().__init__()
        self.tool = Tools()
        self.app = Application()
        self._d = App()._d

    def init(self):
        sAction = App().sAction
        if (sAction=='hm'):
            self.app.homeLeftBtn()
            self.app.fireBtn()
        elif (sAction=='cj'):
            self.app.homeLeftBtn()
            self.app.Lottery()
            self.app.TimeLimitedTask()
            self.app.Turntable()
            self.app.whileMove()
        elif (sAction=='test'):
            self.app.addCount()

action = Action()
action.init()

